package com.example.t1p3;

import java.util.LinkedList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class NewGoalActivity extends Activity {
	static Spinner spinner = null;
	private Game game;
	private Team TeamSelected = null;
	private Player playerGoalSelectedTeam2 = null;
	private Player playerAssist1SelectedTeam2 = null;
	private Player playerAssist2SelectedTeam2 = null;
	private Player playerGoalSelectedTeam1 = null;
	private Player playerAssist1SelectedTeam1 = null;
	private Player playerAssist2SelectedTeam1 = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_goal);
        
    	game = (Game)IntentHelper.getObjectForKey("gameNewGoal");
    	
    	updateSpinnerTeam(); 
    	updateSpinnerGoal();
    	updateSpinnerGoalTeam1();
    	updateSpinnerAssist1();
    	updateSpinnerAssist1Team1();
    	updateSpinnerAssist2();
    	updateSpinnerAssist2Team1();
    }

	private void updateSpinnerTeam() {
		String[] teamName = {game.getTeam1().getNom(), game.getTeam2().getNom()};
    	
		spinner = (Spinner) findViewById(R.id.spinnerTeam);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Team selected = game.getTeam(selectedItem.name);
		    	if(selected != null){
		    		TeamSelected = selected;
		    	}
		    	changeVisiblityAllSpiner();
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {
		        return;
		    } 
		}); 
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, new AdapterHelper[] {	
		    			new AdapterHelper(teamName[0].toString(), "", 0), 
		    			new AdapterHelper(teamName[1].toString(), "", 0)
		    			});
		    
	    spinner.setAdapter(spinnerArrayAdapter);
	    

	}

    private void changeVisiblityAllSpiner() {
    	int visible = Spinner.VISIBLE;
    	int invisible = Spinner.INVISIBLE;
    	
    	if(((Spinner) findViewById(R.id.spinnerGoal)).getVisibility() == visible)
    		((Spinner) findViewById(R.id.spinnerGoal)).setVisibility(invisible);
    	else
    		((Spinner) findViewById(R.id.spinnerGoal)).setVisibility(visible);
    		
    	if(((Spinner) findViewById(R.id.spinnerGoalTeam1)).getVisibility() == visible)
    		((Spinner) findViewById(R.id.spinnerGoalTeam1)).setVisibility(invisible);
    	else
    		((Spinner) findViewById(R.id.spinnerGoalTeam1)).setVisibility(visible);
    	
    	if(((Spinner) findViewById(R.id.spinnerAssist1)).getVisibility() == visible)
    		((Spinner) findViewById(R.id.spinnerAssist1)).setVisibility(invisible);
    	else
    		((Spinner) findViewById(R.id.spinnerAssist1)).setVisibility(visible);
    	
    	if(((Spinner) findViewById(R.id.spinnerAssist1Team1)).getVisibility() == visible)
    		((Spinner) findViewById(R.id.spinnerAssist1Team1)).setVisibility(invisible);
    	else
    		((Spinner) findViewById(R.id.spinnerAssist1Team1)).setVisibility(visible);
    	
    	if(((Spinner) findViewById(R.id.spinnerAssist2)).getVisibility() == visible)
    		((Spinner) findViewById(R.id.spinnerAssist2)).setVisibility(invisible);
    	else
    		((Spinner) findViewById(R.id.spinnerAssist2)).setVisibility(visible);
    	
    	if(((Spinner) findViewById(R.id.spinnerAssist2Team1)).getVisibility() == visible)
    		((Spinner) findViewById(R.id.spinnerAssist2Team1)).setVisibility(invisible);
    	else
    		((Spinner) findViewById(R.id.spinnerAssist2Team1)).setVisibility(visible);
	}

    public void cancel(View view) {
    	Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }
    
    public void ok(View view) {
    	String errorMsg = "";
    	Player playerGoal = null;
    	Player playerAssist1 = null;
    	Player playerAssist2 = null;
    	Team team = null;
    	
    	if(TeamSelected == null)
    		TeamSelected = game.getTeam1();
    	team = TeamSelected;
    	
    	if(TeamSelected.equals(game.getTeam1())){
    		playerGoal = playerGoalSelectedTeam1;
    		playerAssist1 = playerAssist1SelectedTeam1;
    		playerAssist2 = playerAssist2SelectedTeam1;
    	}else if(TeamSelected.equals(game.getTeam2())){

    		playerGoal = playerGoalSelectedTeam2;
    		playerAssist1 = playerAssist1SelectedTeam2;
    		playerAssist2 = playerAssist2SelectedTeam2;
    	}else {
    		errorMsg += "Please select a valid team.\n";
    	}
    	
    	if(playerGoal == null)
    		errorMsg += "Please select a valid Player for the goal.\n";
    	else{
    		if(playerGoal.equals(playerAssist1) || playerGoal.equals(playerAssist2)){
        		errorMsg += "A player can't have the goal and an assist.\n";
        	}
    	}
    	if(playerAssist1 == null && playerAssist2 != null){
    		playerAssist1 = playerAssist2;
    		playerAssist2 = null;
    	}
    	if(playerAssist1 != null && playerAssist1.equals(playerAssist2)){
    		errorMsg += "A player can't be the 2 assist.\n";
    	}
    	
    	
    	if (errorMsg.length() > 0){
    		AlertDialog.Builder alert = new AlertDialog.Builder(this);
    		alert.setTitle("ERREUR");
    		alert.setMessage(errorMsg);
    		alert.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {                
    	        public void onClick(DialogInterface dialog, int which) {
    	        }
    		 });
    		alert.show();
    	}
    	else{
    		Goal goal = new Goal(team, playerGoal, playerAssist1, playerAssist2, game.getPeriod());

        	IntentHelper.addObjectForKey(goal, "goal");
            setResult(RESULT_OK, new Intent());
            finish();
    	}
    }
    
    private void updateSpinnerGoal(){
		spinner = (Spinner) findViewById(R.id.spinnerGoal);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Player selected = game.getTeam2().getPLayer(selectedItem.number);
		    	if(selected != null){
		    		playerGoalSelectedTeam2 = selected;
		    	}
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {return;} 
		}); 
		
		AdapterHelper[] adapter = getPlayerForTeam2(false);
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, adapter);

	    spinner.setAdapter(spinnerArrayAdapter); 	

    }
    
    private void updateSpinnerGoalTeam1(){
		spinner = (Spinner) findViewById(R.id.spinnerGoalTeam1);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Player selected = game.getTeam1().getPLayer(selectedItem.number);
		    	if(selected != null){
		    		playerGoalSelectedTeam1 = selected;
		    	}
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {return;} 
		}); 
		
		AdapterHelper[] adapter = getPlayerForTeam1(false);
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, adapter);

	    spinner.setAdapter(spinnerArrayAdapter);
    }    

    private void updateSpinnerAssist1(){
		spinner = (Spinner) findViewById(R.id.spinnerAssist1);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Player selected = game.getTeam2().getPLayer(selectedItem.number);
		    	if(selected != null){
		    		playerAssist1SelectedTeam2 = selected;
		    	}else
		    		playerAssist1SelectedTeam2 = null;
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {return;} 
		}); 
		
		AdapterHelper[] adapter = getPlayerForTeam2(true);
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, adapter);

	    spinner.setAdapter(spinnerArrayAdapter); 	

    }
    
    private void updateSpinnerAssist1Team1(){
		spinner = (Spinner) findViewById(R.id.spinnerAssist1Team1);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Player selected = game.getTeam1().getPLayer(selectedItem.number);
		    	if(selected != null){
		    		playerAssist1SelectedTeam1 = selected;
		    	}else
		    		playerAssist1SelectedTeam1 = null;
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {return;} 
		}); 
		
		AdapterHelper[] adapter = getPlayerForTeam1(true);
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, adapter);

	    spinner.setAdapter(spinnerArrayAdapter); 	

    }
    
    private void updateSpinnerAssist2(){
		spinner = (Spinner) findViewById(R.id.spinnerAssist2);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Player selected = game.getTeam2().getPLayer(selectedItem.number);
		    	if(selected != null){
		    		playerAssist2SelectedTeam2 = selected;
		    	}else
		    		playerAssist2SelectedTeam2 = null;
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {return;} 
		}); 
		
		AdapterHelper[] adapter = getPlayerForTeam2(true);
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, adapter);

	    spinner.setAdapter(spinnerArrayAdapter); 	

    }
    
    private void updateSpinnerAssist2Team1(){
		spinner = (Spinner) findViewById(R.id.spinnerAssist2Team1);
		
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		    	AdapterHelper selectedItem = (AdapterHelper) adapterView.getItemAtPosition(i);
		    	Player selected = game.getTeam1().getPLayer(selectedItem.number);
		    	if(selected != null){
		    		playerAssist2SelectedTeam1 = selected;
		    	}else
		    		playerAssist2SelectedTeam1 = null;
		    } 

			public void onNothingSelected(AdapterView<?> adapterView) {return;} 
		}); 
		
		AdapterHelper[] adapter = getPlayerForTeam1(true);
		
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
		          android.R.layout.simple_spinner_item, adapter);

	    spinner.setAdapter(spinnerArrayAdapter); 	

    }
    
    private AdapterHelper[] getPlayerForTeam1(boolean noneOption) {
    	LinkedList<Player> listPlayerTeam1 = game.getTeam1().getJoueurs();
    	AdapterHelper[] adapter;
    	if(noneOption)
    		adapter = new AdapterHelper[listPlayerTeam1.size()+1];
    	else
    		adapter = new AdapterHelper[listPlayerTeam1.size()];
		for (int i = 0; i < listPlayerTeam1.size(); i++) {
			adapter[i] = new AdapterHelper(listPlayerTeam1.get(i).getFirstName(), 
					listPlayerTeam1.get(i).getLastName(), 
					listPlayerTeam1.get(i).getNumber());
		}
		if(noneOption)
			adapter[listPlayerTeam1.size()] =  new AdapterHelper("none", "", 0);
		
		return adapter;
	} 
    
    private AdapterHelper[] getPlayerForTeam2(boolean noneOption) {
    	LinkedList<Player> listPlayerTeam2 = game.getTeam2().getJoueurs();
    	
    	AdapterHelper[] adapter;
    	if(noneOption)
    		adapter = new AdapterHelper[listPlayerTeam2.size()+1];
    	else
    		adapter = new AdapterHelper[listPlayerTeam2.size()];
		for (int i = 0; i < listPlayerTeam2.size(); i++) {
			adapter[i] = new AdapterHelper(listPlayerTeam2.get(i).getFirstName(), 
					listPlayerTeam2.get(i).getLastName(), 
					listPlayerTeam2.get(i).getNumber());
		}
		if(noneOption)
			adapter[listPlayerTeam2.size()] =  new AdapterHelper("none", "", 0);
		
		return adapter;
	}
}
